package com.example.team.dao.Imp;

import com.example.team.dao.ISafetyEquipmentPurchaseDetailsDao;
import com.example.team.entity.SafetyEquipmentPurchaseDetails;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 *根据接口操作数据库表的接口规范，对其进行实现
 * 编写者：蓝江富
 * 时间：2023年10月31日16:12:47
 */
@Repository
public class ISafetyEquipmentPurchaseDetailsDaoImpl implements ISafetyEquipmentPurchaseDetailsDao {
    @Autowired
    private JdbcTemplate jdbcTemplate;
    @Override
    public int insertSafetyEquipmentPurchaseDetails(SafetyEquipmentPurchaseDetails safetyEquipmentPurchaseDetails) {
        return jdbcTemplate.update(
                "insert into safety_equipment_purchase_details(" +
                        "detailsId," +
                        "itemName," +
                        "quantity," +
                        "unitPrice," +
                        "TotalPrice," +
                        "applicant," +
                        "approve " +
                        ") " +
                        "values(?,?,?,?,?,?,?)"
                ,safetyEquipmentPurchaseDetails.getDetailsId()
                ,safetyEquipmentPurchaseDetails.getItemName()
                ,safetyEquipmentPurchaseDetails.getQuantity()
                ,safetyEquipmentPurchaseDetails.getUnitPrice()
                ,safetyEquipmentPurchaseDetails.getTotalPrice()
                ,safetyEquipmentPurchaseDetails.getApplicant()
                ,safetyEquipmentPurchaseDetails.getApprove()
        );
    }

    @Override
    public int deleteSafetyEquipmentPurchaseDetails(SafetyEquipmentPurchaseDetails safetyEquipmentPurchaseDetails) {
        return jdbcTemplate.update(
                "delete from safety_equipment_purchase_details " +
                "where detailsId=?",safetyEquipmentPurchaseDetails.getDetailsId());
    }

    @Override
    public int updateSafetyEquipmentPurchaseDetails(SafetyEquipmentPurchaseDetails safetyEquipmentPurchaseDetails) {
        //如果更新的字段为空，则保留旧数据
        SafetyEquipmentPurchaseDetails safetyEquipmentPurchaseDetails1=getSafetyEquipmentPurchaseDetailsById(safetyEquipmentPurchaseDetails);
        if(safetyEquipmentPurchaseDetails.getItemName()==null)
            safetyEquipmentPurchaseDetails.setItemName(safetyEquipmentPurchaseDetails1.getItemName());
        if(safetyEquipmentPurchaseDetails.getQuantity()==null)
            safetyEquipmentPurchaseDetails.setQuantity(safetyEquipmentPurchaseDetails1.getQuantity());
        if(safetyEquipmentPurchaseDetails.getUnitPrice()==null)
            safetyEquipmentPurchaseDetails.setUnitPrice(safetyEquipmentPurchaseDetails1.getUnitPrice());
        if(safetyEquipmentPurchaseDetails.getTotalPrice()==null)
            safetyEquipmentPurchaseDetails.setTotalPrice(safetyEquipmentPurchaseDetails1.getTotalPrice());
        if(safetyEquipmentPurchaseDetails.getApplicant()==null)
            safetyEquipmentPurchaseDetails.setApplicant(safetyEquipmentPurchaseDetails1.getApplicant());
        if(safetyEquipmentPurchaseDetails.getApprove()==null)
            safetyEquipmentPurchaseDetails.setApprove(safetyEquipmentPurchaseDetails1.getApprove());
        return jdbcTemplate.update("update safety_equipment_purchase_details set " +
                        "itemName=?," +
                        "quantity=?," +
                        "unitPrice=?," +
                        "TotalPrice=?," +
                        "applicant=?," +
                        "approve=? " +
                        "where detailsId=?"
                ,safetyEquipmentPurchaseDetails.getItemName()
                ,safetyEquipmentPurchaseDetails.getQuantity()
                ,safetyEquipmentPurchaseDetails.getUnitPrice()
                ,safetyEquipmentPurchaseDetails.getTotalPrice()
                ,safetyEquipmentPurchaseDetails.getApplicant()
                ,safetyEquipmentPurchaseDetails.getApprove()
                ,safetyEquipmentPurchaseDetails.getDetailsId()
        );
    }

    @Override
    public List<SafetyEquipmentPurchaseDetails> getAllSafetyEquipmentPurchaseDetails(SafetyEquipmentPurchaseDetails safetyEquipmentPurchaseDetails) {
        if(safetyEquipmentPurchaseDetails.getApplicant()!=null)
            return jdbcTemplate.query("select " +
                            "detailsId," +
                            "itemName," +
                            "quantity," +
                            "unitPrice," +
                            "TotalPrice," +
                            "applicant," +
                            "approve " +
                "from safety_equipment_purchase_details where applicant='"+safetyEquipmentPurchaseDetails.getApplicant()+"'",new Object[]{},
                new BeanPropertyRowMapper<>(SafetyEquipmentPurchaseDetails.class));
        else
            return jdbcTemplate.query("select " +
                            "detailsId," +
                            "itemName," +
                            "quantity," +
                            "unitPrice," +
                            "TotalPrice," +
                            "applicant," +
                            "approve " +
                "from safety_equipment_purchase_details",new Object[]{},
                new BeanPropertyRowMapper<>(SafetyEquipmentPurchaseDetails.class));
    }

    @Override
    public SafetyEquipmentPurchaseDetails getSafetyEquipmentPurchaseDetails(SafetyEquipmentPurchaseDetails safetyEquipmentPurchaseDetails) {
        List<SafetyEquipmentPurchaseDetails> result=jdbcTemplate.query("select " +
                        "detailsId," +
                        "itemName," +
                        "quantity," +
                        "unitPrice," +
                        "TotalPrice," +
                        "applicant," +
                        "approve " +
                        "from safety_equipment_purchase_details where applicant='"+safetyEquipmentPurchaseDetails.getApplicant()+"'",
                        new Object[]{},
                        new BeanPropertyRowMapper<>(SafetyEquipmentPurchaseDetails.class));
        if (result.size()>=1)
            return result.get(0);
        else return null;
    }

    @Override
    public SafetyEquipmentPurchaseDetails getSafetyEquipmentPurchaseDetailsById(SafetyEquipmentPurchaseDetails safetyEquipmentPurchaseDetails) {
        List<SafetyEquipmentPurchaseDetails> result=jdbcTemplate.query("select " +
                        "detailsId," +
                        "itemName," +
                        "quantity," +
                        "unitPrice," +
                        "TotalPrice," +
                        "applicant," +
                        "approve " +
                        "from safety_equipment_purchase_details where detailsId='"+safetyEquipmentPurchaseDetails.getDetailsId()+"'",
                        new Object[]{},
                        new BeanPropertyRowMapper<>(SafetyEquipmentPurchaseDetails.class));
        if (result.size()>=1)
            return result.get(0);
        else return null;
    }
}
